package ms.unicorn.modellanguage;

import ms.unicorn.Element;
import ms.unicorn.Relation;
import ms.unicorn.diagram.providers.UnicornElementTypes;
import ms.unicorn.language.languagedef.ConnectionStyleDef;
import ms.unicorn.language.languagedef.LanguageDefUtil;
import ms.unicorn.language.languagedef.RelationConstraint;
import ms.unicorn.language.languagedef.RelationDef;
import ms.unicorn.language.languagedef.RelationDefUtil;
import ms.unicorn.language.languagedef.RelationViewDef;

import org.eclipse.gmf.runtime.emf.type.core.IElementType;

public class LanguageRelation extends LanguageElement {

	private IElementEditHelperAdvice advice;
	
	/*static {
		lineStyles.put("solid", SWT.LINE_SOLID);
		lineStyles.put("dash", SWT.LINE_DASH);
		lineStyles.put("dashdot", SWT.LINE_DASHDOT);
		lineStyles.put("dashdotdot", SWT.LINE_DASHDOTDOT);
		lineStyles.put("dot", SWT.LINE_DOT);
		
		lineCaps.put("flat", SWT.CAP_FLAT);
		lineCaps.put("round", SWT.CAP_ROUND);
		lineCaps.put("square", SWT.CAP_SQUARE);
		
		lineJoins.put("bevel", SWT.JOIN_BEVEL);
		lineJoins.put("miter", SWT.JOIN_MITER);
		lineJoins.put("round", SWT.JOIN_ROUND);
	}*/

	
	public LanguageRelation(IModelLanguage language, RelationDef definition) {
		super(language, definition);
	}
		
	@Override
	protected IElementType getBaseType() {
		return UnicornElementTypes.Relation_4001; 
	}
	
	public boolean checkRelationConstraints(Element container,
			Relation linkInstance, Element source, Element target) {
		LanguageElement src = LanguageRegistry.INSTANCE.getLanguageElement(source);
		LanguageElement tar = LanguageRegistry.INSTANCE.getLanguageElement(target);
		
		if (src == null || tar == null || source == target)
			return true;
		
		RelationViewDef view = (RelationViewDef) LanguageDefUtil.resolveDefinition(getDefinition().getView());
		
		RelationConstraint constraint = RelationDefUtil.resolveConstraint(src, tar, view.getConstraints());
		
		
		boolean result = constraint != null && !constraint.isForbidden();
		
		IElementEditHelperAdvice helper = getEditHelperAdvice();
		if (result && helper instanceof LanguageRelationEditHelperAdvice) {
			result = ((LanguageRelationEditHelperAdvice) helper).checkRelationConstraints(container,
					linkInstance, source, target);
		}
		
		return result;
	}
	
	public ConnectionStyleDef getConnectionStyle(Element source, Element target) {
		LanguageElement src = LanguageRegistry.INSTANCE.getLanguageElement(source);
		LanguageElement tar = LanguageRegistry.INSTANCE.getLanguageElement(target);
		
		RelationViewDef view = (RelationViewDef) LanguageDefUtil.resolveDefinition(getDefinition().getView());
		
		RelationConstraint constraint = RelationDefUtil.resolveConstraint(src, tar, view.getConstraints());
		ConnectionStyleDef style = (ConnectionStyleDef) (constraint != null && constraint.getStyle() != null ? 
				LanguageDefUtil.resolveDefinition(constraint.getStyle()) : null);
		if (style == null && view.getStyle() != null) 
			style = LanguageDefUtil.resolveDefinition(view.getStyle()) ;
		return style;
	}
		
	public void setEditHelperAdvice(IElementEditHelperAdvice advice) {
		this.advice = advice;
	}

	public IElementEditHelperAdvice getEditHelperAdvice() {
		return advice != null ? advice : LanguageRelationEditHelperAdvice.DefaultInstance;
	}

}
